# Sustainable Office Designer
# by Fabian Ritter <Fabian.Ritter@live.de>, 2012

require 'SOD/tools/OpenStudio/Collection'
require("SOD/tools/observers/FootprintObserver")

module SOD
  
  class Footprint
    
    attr_reader :footprints, :cores
    attr_reader :fpPointArray, :corePointArray
    
    def initialize
      @footprints = Collection.new
      @cores = Collection.new
      @fpPointArray = Collection.new
      @corePointArray = Collection.new
    end
    
    def collectNew
      
      @footprints.clear
      @cores.clear
      @fpPointArray.clear
      @corePointArray.clear
      
      collect
      
    end
    
    def collect
      
      ents.each do |e|
        e_type =  e.get_attribute "SOD_dictionary", "SOD_type"
        @footprints.add(e) if e_type == "Footprint"
        @cores.add(e) if e_type == "Core"
      end
      
      @footprints.each do |fp|
        pointArray = []
        fpVertices = fp.vertices
        fpVertices.each { |fpv| pointArray << fpv.position }
        
        @fpPointArray.add(pointArray)
      end
      
      @cores.each do |core|
        pointArray = []
        coreVertices = core.vertices
        coreVertices.each { |cv| pointArray << cv.position }
        
        @corePointArray.add(pointArray)
      end
      
    end
    
    def draw
      
      delete
      
      @fpPointArray.each do |fpVertices|
        fp = Sketchup.active_model.active_entities.add_face fpVertices
        Plugin.sodLayers.makeFootprint(fp)
      end
      
      @corePointArray.each do |coreVertices|
        core = Sketchup.active_model.active_entities.add_face coreVertices
        Plugin.sodLayers.makeCoreZone(core)
      end
      
    end
    
  #FR: have to think about this part!
  def add(face)
    Sketchup.active_model.start_operation("New Footprint")       
      Plugin.sodLayers.makeFootprint(face)
      Plugin.model.footprint.collectNew
      footprints.each { |fp| puts fp.get_attribute("SOD_dictionary", "SOD_type") }
      @footprintObserver = FootprintObserver.new
      face.add_observer @footprintObserver
    Sketchup.active_model.commit_operation
  end
  
  def remove(face)
    Sketchup.active_model.start_operation("Remove Footprint")
      Plugin.sodLayers.removeFootprint(face)
      Plugin.model.footprint.collectNew
    Sketchup.active_model.commit_operation
  end
  
  def addCore(face)
    Sketchup.active_model.start_operation("Place Core Zone")       
      Plugin.sodLayers.makeCoreZone(face)
      Plugin.model.footprint.collectNew
      cores.each { |core| puts core.get_attribute("SOD_dictionary", "SOD_type") }
    Sketchup.active_model.commit_operation
  end
    
    def return
      return @footprints
    end
    
    def to_s
      self.each.to_s
    end
    
    def delete
      
      Sketchup.active_model.active_entities.each do |e|
        e_type =  e.get_attribute "SOD_dictionary", "SOD_type"
        @footprints.add(e) if e_type == "Footprint"
        @cores.add(e) if e_type == "Core"
        Sketchup.active_model.active_entities.erase_entities e if e_type
      end
      
    end
    
  end
  
end
